import { defineStore } from 'pinia'

export const usePersistenceStore = defineStore('persistence', {
  state: () => ({
    username: '',
    theme: ''
  }),
  
  getters: {
    preferences: () => {
      const saved = localStorage.getItem('userPreferences')
      return saved ? JSON.parse(saved) : null
    }
  },
  
  actions: {
    savePreferences() {
      const preferences = {
        username: this.username,
        theme: this.theme,
        savedAt: new Date().toISOString()
      }
      localStorage.setItem('userPreferences', JSON.stringify(preferences))
    },
    clearPreferences() {
      localStorage.removeItem('userPreferences')
    },
    loadPreferences() {
      const saved = localStorage.getItem('userPreferences')
      if (saved) {
        const preferences = JSON.parse(saved)
        this.username = preferences.username || ''
        this.theme = preferences.theme || ''
      }
    }
  }
})